package com.myc.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myc.train.common.exception.BusinessException;
import com.myc.train.common.exception.BusinessExceptionEnum;
import com.myc.train.common.resp.PageResp;
import com.myc.train.common.utils.SnowUtil;
import com.myc.train.business.domain.Station;
import com.myc.train.business.domain.StationExample;
import com.myc.train.business.mapper.StationMapper;
import com.myc.train.business.req.StationQueryReq;
import com.myc.train.business.req.StationSaveReq;
import com.myc.train.business.resp.StationQueryResp;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StationService {

    @Resource
    private StationMapper stationMapper;

    public void save(StationSaveReq req){
        DateTime now = DateTime.now();
        Station station = BeanUtil.copyProperties(req, Station.class);

        if(ObjectUtil.isNull(station.getId())){
            //判断车站是否已存在
            Station stations = selectByUnique(req.getName());
            if(ObjectUtil.isNotEmpty(stations)){
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_STATION_NAME_ERROR);
            }
            //不存在则进行赋值新增
            station.setId(SnowUtil.getSnowflakeNextId());
            station.setCreateTime(now);
            station.setUpdateTime(now);
            stationMapper.insert(station);
        }else{
            station.setUpdateTime(now);
            stationMapper.updateByPrimaryKey(station);
        }
    }

    //按照唯一键查询数据
    private Station selectByUnique(String name) {
        StationExample stationExample = new StationExample();
        stationExample.createCriteria().andNameEqualTo(name);
        List<Station> stations = stationMapper.selectByExample(stationExample);
        if(CollUtil.isNotEmpty(stations)){
            return stations.get(0);
        }else {
            return null;
        }
    }

    public PageResp<StationQueryResp> queryList(StationQueryReq req){
       //定义查询条件
        StationExample stationExample = new StationExample();
        //根据id倒序排序
        stationExample.setOrderByClause("id desc");
        //创建查询对象
        StationExample.Criteria criteria = stationExample.createCriteria();
        //不为空则对其新增查询条件


        //新增分页信息
        PageHelper.startPage(req.getPage(),req.getSize());
        //根据条件查询结果
        List<Station> stations = stationMapper.selectByExample(stationExample);
        //new一个PageInfo并将结果放进来
        PageInfo<Station> stationPageInfo = new PageInfo<>(stations);
        //类拷贝
        List<StationQueryResp> queryResps = BeanUtil.copyToList(stations, StationQueryResp.class);
        //new一个PageResp
        PageResp<StationQueryResp> resp= new PageResp<>();
        //放数据
        resp.setTotal(stationPageInfo.getTotal());
        resp.setList(queryResps);


        return resp;
    }

    public void delete(Long id){
        stationMapper.deleteByPrimaryKey(id);
    }


    public List<StationQueryResp> queryAll() {
       //定义查询条件
        StationExample stationExample = new StationExample();
        stationExample.setOrderByClause("name_pinyin asc");
        //查询
        List<Station> stations = stationMapper.selectByExample(stationExample);
        //类拷贝成输出格式
        return BeanUtil.copyToList(stations , StationQueryResp.class);
    }
}
